package 富婆梦.第一阶段.第八周Scoket.作业3;

import java.io.*;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第八周Scoket.作业3
 * @ClassName: CopyPicture
 * @Author: hemengjin
 * @Description: 拷贝一张图片，从一个目录到另外一个目录下(PS:是拷贝是不是移动)
 * @Date:2021/9/23 16:00
 * @Version: 1.0
 */
public class CopyPicture {

    //在老目录下找到图片,并以二进制的形式存储为一个byte数组
    public static byte[] readPicture() {

        File file = new File("D:\\何梦锦的宝藏\\Java学习\\java-learning\\1.jpg");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStream inputStream = new BufferedInputStream(fileInputStream);
            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);
            inputStream.close();
            return b;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    //将存储图片的byte数组复制到新目录下
    public static void writePicture( byte[] bytes) {
        File file = new File("D:\\何梦锦的宝藏\\Java学习\\java-learning\\src\\富婆梦\\第一阶段\\第八周Scoket\\作业3\\1.jpg");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            OutputStream outputStream = new BufferedOutputStream(fileOutputStream);
            outputStream.write(bytes);
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        writePicture(readPicture());
        File file = new File("D:\\何梦锦的宝藏\\Java学习\\java-learning\\src\\富婆梦\\第一阶段\\第八周Scoket\\作业3");
        File[] list = file.listFiles();
        for (File f:list){
            if (f.getName().endsWith(".jpg")) {
                System.out.println("copy图片成功！");
            }
        }

    }
}
